package com.hyt.it.ogt.ks.mq;

import com.hyt.it.ogt.ks.websocket.CandidateWebSocketService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * 考中发消息
 * @author 刘恒活
 * @since 2020/7/10 16:31
 */
@Component
@Slf4j
public class TestingSendMessageConsumer {
    @Resource
    private CandidateWebSocketService candidateWebSocketService;

    @RabbitListener(bindings = {@QueueBinding(value = @Queue(autoDelete = "true", durable = "true"),
            exchange = @Exchange(value = "${mq.topic.exchange}", type = ExchangeTypes.TOPIC),
            key = {"${mq.topic.testingMessage.route}"})},
            concurrency = "${mq.topic.testingMessage.concurrency}"
    )
    public void testingSendMessage(String json, Message message, Channel channel) {
        try {
            log.info("考中发消息MQ监听消息内容:【{}】", json);
            candidateWebSocketService.sendMessage(json);
        } catch (Exception e) {
            log.error("考中发消息异常: 【{}】------{}", json, e);
        } finally {
            try {
                // 确认消费一条消息
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } catch (IOException e) {
                log.error("考中发消息异常: 【{}】------{}", json, e);
            }
        }
    }
}
